//
//  PopularForecastFromModel.m
//  Gxj
//
//  Created by 刘伟 on 2017/10/13.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "PopularForecastFromModel.h"

NSString *const kPopularForecastMsg       = @"msg";
NSString *const kPopularForecastList      = @"list";
NSString *const kPopularForecastPagecount = @"pagecount";
NSString *const kPopularForecastResstate  = @"resstate";

@implementation PopularForecastFromModel

@synthesize msg        = _msg;
@synthesize list       = _list;
@synthesize pagecount  = _pagecount;
@synthesize resstate   = _resstate;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}
// 初始化
- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    if (self && [dict isKindOfClass:[NSDictionary class]])
    {
        _msg        = [self checkNilObjectForKey:kPopularForecastMsg                 fromDictionary:dict];
        _resstate   = [self checkNilObjectForKey:kPopularForecastResstate            fromDictionary:dict];
        _list       = [self dataList:[self checkNilObjectForKey:kPopularForecastList fromDictionary:dict]];
        _pagecount  = [self checkNilObjectForKey:kPopularForecastPagecount           fromDictionary:dict];
    }
    return self;
}

- (NSArray *)dataList:(id)sender
{
    NSMutableArray *parseDataList = [NSMutableArray array];
    if ([sender isKindOfClass:[NSArray class]])
    {
        for (NSDictionary *item in (NSArray *)sender)
        {
            [parseDataList addObject:[PopularForecastFromModel_Two modelObjectWithDictionary:item]];
        }
    }
    else if ([sender isKindOfClass:[NSDictionary class]])
    {
        [parseDataList addObject:[PopularForecastFromModel_Two modelObjectWithDictionary:(NSDictionary *)sender]];
    }
    return parseDataList;
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:_msg                                  forKey:kPopularForecastMsg];
    [mutableDict setValue:_resstate                             forKey:kPopularForecastResstate];
    [mutableDict setValue:_pagecount                            forKey:kPopularForecastPagecount];
    
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (NSDictionary *dic in _list)
    {
        if ([dic respondsToSelector:@selector(dictionaryRepresentation)])
        {
            [mutableArray addObject:[dic performSelector:@selector(dictionaryRepresentation)]];
        }
        else
        {
            [mutableArray addObject:dic];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:mutableArray] forKey:kPopularForecastList];
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (id)checkNilObjectForKey:(id)key fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:key];
    return [object isKindOfClass:[NSNull class]] ? nil : object;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@",[self dictionaryRepresentation]];
}


@end
